﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;
using System.Security.Cryptography;

namespace Transport
{
    public class NguoiDungControl
    {
        transportEntities db;

        public NguoiDungControl()
        {
            db = new transportEntities();
        }

        public List<nguoi_dung> listAll()
        {
            try
            {
                return db.nguoi_dung.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<nguoi_dung> listAllPartner()
        {
            try
            {
                return db.nguoi_dung.Where(t => t.loai_nguoi_dung == "PARTNER").ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public nguoi_dung findByUsername(string username)
        {
            try
            {
                return db.nguoi_dung.SingleOrDefault(t => t.ten_dang_nhap == username);
            }
            catch (Exception)
            {
                return null;
            }
        }
        public nguoi_dung findByEmail(string email)
        {
            try
            {
                return db.nguoi_dung.SingleOrDefault(t => t.email == email);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string getEmail(string username)
        {
            try
            {
                nguoi_dung ng = db.nguoi_dung.SingleOrDefault(t => t.ten_dang_nhap == username);
                if (ng != null)
                    return ng.email;
                else
                    return "";
            }
            catch (Exception)
            {
                return "";
            }
        }

        public bool resetPassword(string username)
        {
            try
            {
                nguoi_dung nd = findByUsername(username);

                if (nd != null)
                {
                    HashAlgorithm alg = new MD5CryptoServiceProvider();
                    string newPwd = DateTime.Now.Ticks.ToString();
                    byte[] bte = alg.ComputeHash(System.Text.Encoding.UTF8.GetBytes(newPwd));
                    string pwd = BitConverter.ToString(bte).Replace("-", "").ToLower();

                    Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
                    MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

                    String fromEmail = mailSettings.Smtp.Network.UserName;
                    String toEmail = getEmail(username);
                    string host = mailSettings.Smtp.Network.Host;
                    int port = mailSettings.Smtp.Network.Port;
                    string password = mailSettings.Smtp.Network.Password;
                    MailMessage message = new MailMessage(fromEmail, toEmail);
                    message.Subject = "[HKTransport] Reset Password";
                    message.Body = "Kính chào quý khách,\nMật khẩu mới của bạn là " + newPwd + "\nChúng tôi rất hân hạnh được phục vụ quý khách.\nTrung tâm hỗ trợ HK Transport";

                    SmtpClient smtp = new SmtpClient(host, port);
                    smtp.EnableSsl = true;
                    smtp.Credentials = new NetworkCredential(fromEmail, password);
                    smtp.Send(message);

                    nd.password = pwd;
                    update(nd);

                    return true;
                }
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool insert(nguoi_dung value)
        {
            try
            {
                db.nguoi_dung.AddObject(value);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool update(nguoi_dung value)
        {
            try
            {
                nguoi_dung nd = db.nguoi_dung.SingleOrDefault(t => t.ten_dang_nhap == value.ten_dang_nhap);
                nd = value;
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool delete(string username)
        {
            try
            {
                nguoi_dung nd = db.nguoi_dung.SingleOrDefault(t => t.ten_dang_nhap == username);
                db.nguoi_dung.DeleteObject(nd);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool checkLogin(string username, string password)
        {
            nguoi_dung nd = db.nguoi_dung.SingleOrDefault(t => t.ten_dang_nhap == username);

            if (nd != null)
            {
                HashAlgorithm alg = new MD5CryptoServiceProvider();
                byte[] bte = alg.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
                string pwd = BitConverter.ToString(bte).Replace("-", "").ToLower();

                if (nd.password == pwd)
                    return true;
            }

            return false;
        }

    }
}
